﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Enterprise.Model.Vote;
using Enterprise.Data.Vote;
using Enterprise.Infrastructure;
namespace Enterprise.Service.Vote
{
    public class VoteService
    {
        /// <summary>
        /// 用户缓存标记
        /// </summary>
        private static string cacheKey = "Enterprise-Vote";

        /// <summary>
        /// 列表 返回所有投票
        /// </summary>
        /// <returns></returns>
        public List<VoteEntity> VoteList()
        {
            if (CacheHelper.Contains(cacheKey))
            {
                return (List<VoteEntity>)CacheHelper.GetCache(cacheKey);
            }
            else
            {
                VoteData data = new VoteData();
                List<VoteEntity> list = data.VoteList();
                CacheHelper.Add(cacheKey, list);
                return list;
            }
        }

        /// <summary>
        /// 留言显示
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public VoteEntity VoteDisp(string voteId)
        {
            return VoteList().Where(p => p.VID == voteId).FirstOrDefault();
        }

        /// <summary>
        /// 操作 添加、修改、删除
        /// </summary>
        /// <param name="entity">经理信箱实体</param>
        public void VoteExecute(VoteEntity entity)
        {
            string cmd = string.Empty;
            switch (entity.DB_Option_Action)
            {
                case "Insert":
                    cmd = "Insert into VOTE_INFO(VID,VTITLE,VTYPE,VENDTIME,VRANGE,VREMARK,VCREATEUSER,VCREATETIME,VMIX,VMAX,VPUBLIC,VPUBLICTIME) values('" + entity.VID + "','" + entity.VTITLE + "','" + entity.VTYPE + "'," + Utility.FormatOracleDate(entity.VENDTIME) + ",'" + entity.VRANGE + "','" + entity.VREMARK + "'," + entity.VCREATEUSER + "," + Utility.FormatOracleDate(entity.VCREATETIME) + "," + entity.VMIX + "," + entity.VMAX + "," + entity.VPUBLIC + "," + Utility.FormatOracleDate(entity.VPUBLICTIME) + ")";
                    break;
                case "Update":
                    cmd = "Update VOTE_INFO set VTITLE='" + entity.VTITLE + "',VTYPE='" + entity.VTYPE + "',VENDTIME=" + Utility.FormatOracleDate(entity.VENDTIME) + ",VRANGE='" + entity.VRANGE + "',VREMARK='" + entity.VREMARK + "',VCREATEUSER=" + entity.VCREATEUSER + ",VCREATETIME=" + Utility.FormatOracleDate(entity.VCREATETIME) + ",VMIX=" + entity.VMIX + ",VMAX=" + entity.VMAX + ",VPUBLIC=" + entity.VPUBLIC + ",VPUBLICTIME=" + Utility.FormatOracleDate(entity.VPUBLICTIME) + " where VID=" + entity.VID;
                    break;
                case "Delete":
                    cmd = "Delete from VOTE_INFO where VID='" + entity.VID;
                    break;
            }
            if (!string.IsNullOrEmpty(cmd))
            {
                VoteExecuteBySql(cmd);
            }
        }

        /// <summary>
        /// 操作  
        /// </summary>
        /// <param name="cmd">sql语句</param>
        public void VoteExecuteBySql(string cmd)
        {
            VoteData data = new VoteData();
            data.Execute(cmd);
            CacheHelper.RemoveCache(cacheKey);
        }
    }
}
